package ex_11_setNewLock;

public class ChangeLockProperty implements Runnable{
    ChangeLockProperty.Inner inner = new Inner();

    @Override
    public void run() {
        try {
            synchronized (inner){
                System.out.println(Thread.currentThread().getName() + " begin ");
                inner.setName("xxx");
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + " end ");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

     class Inner{
        private String name;

         public String getName() {
             return name;
         }

         public void setName(String name) {
             this.name = name;
         }
     }
    /**
     * =====================================
     * 更改锁对象的属性,多个线程之间同步执行
     *
     * =====================================
     */
    public static void main(String[] args) throws Exception{
        ChangeLockProperty changeLock = new ChangeLockProperty();
        Thread t1 = new Thread(changeLock);
        t1.setName("AA");
        Thread t2 = new Thread(changeLock);
        t2.setName("BB");
        t1.start();
        Thread.sleep(20);
        t2.start();
    }
}
